﻿

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;

namespace HDJ.Framework.Core
{
    public class WeaponBase : MonoBehaviour
    {
        public string weaponName = "";
        public WeaponBaseDamageType weaponBaseDamageType = WeaponBaseDamageType.None;
        /// <summary>
        /// 武器质量
        /// </summary>
        public int mass;
        /// <summary>
        /// 挥动武器只有高于这个速度才会产生伤害
        /// </summary>
        public float damageSpeed = 1f;

        //public Transform damageCheckStartPoint;
        public Transform damageCheckEndPoint;
        //public float damageArea_length = 0.1f;
        //public float damageArea_width = 0.1f;

        public TriggerAreas triggerAreas;
        private void Awake()
        {
            endPointLastPoint = damageCheckEndPoint.position;
            triggerAreas.OnCollisionOneEnterCallBack = CalculateDamageArea;
            OnAwake();
        }
        public Vector3 testRote = new Vector3(0, 10, 0);
        private void Update()
        {
            if (weaponBaseDamageType != WeaponBaseDamageType.None)
            {
                CalculateEndPointSpeed();
                if (damageEndPointSpeed >= damageSpeed)
                {
                    triggerAreas.m_collider.isTrigger = false;
                }
                else
                {
                    triggerAreas.m_collider.isTrigger = true;
                }
            }

            transform.Rotate(testRote);
            OnUpdate();
        }

        public float damageEndPointSpeed = 0;
        private Vector3 endPointLastPoint;
        /// <summary>
        /// 计算伤害检查末端的速度
        /// </summary>
        private void CalculateEndPointSpeed()
        {
            Vector3 dis = damageCheckEndPoint.position - endPointLastPoint;
            damageEndPointSpeed = dis.magnitude / Time.deltaTime;
            endPointLastPoint = damageCheckEndPoint.position;

        }
        public float unPin = 5;
        private void CalculateDamageArea(Collision collider)
        {
            if (damageEndPointSpeed >= damageSpeed)
            {
                //float force = (damageEndPointSpeed * mass);
                //if (weaponBaseDamageType == WeaponBaseDamageType.Blunt)
                //{
                //    var broadcaster = collider.gameObject.GetComponent<MuscleCollisionBroadcaster>();

                //    foreach (var contact in collider.contacts)
                //    {
                //        if (broadcaster != null)
                //        {
                //            broadcaster.Hit(unPin, contact.normal * force, contact.point);

                //        }
                //    }
                //}
                //DamageController.Damage(this.transform, collider.transform, force);
                //Debug.Log("Collision");
                //ContactPoint contact = collider.contacts[0];
                //if (broadcaster != null)
                //{
                //    broadcaster.Hit(unPin, contact.normal * (damageEndPointSpeed * mass), contact.point);

                //}
            }
        }
        public virtual void OnUpdate() { }
        public virtual void OnAwake() { }
    }

    /// <summary>
    /// 武器基本产生伤害的类型
    /// </summary>
    public enum WeaponBaseDamageType
    {
        /// <summary>
        /// 钝器，产生击退 击飞
        /// </summary>
        Blunt,
        /// <summary>
        /// 锐器，产生穿透，切割效果
        /// </summary>
        Sharp,
        /// <summary>
        /// 武器本身无伤害
        /// </summary>
        None,
    }
}